import 'package:flutter/material.dart';
import 'package:flutter_myshop/common/constant.dart';
import 'package:flutter_myshop/common/widget.dart';
import 'package:flutter_myshop/model/user_info.dart';
import 'package:flutter_myshop/service/service_method.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:url_launcher/url_launcher.dart';

/*
 * 用户中心
 */
class MemberPage extends StatelessWidget {
  //cate 变量
  UserInfoModel userInfoModel;

  // main
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("用户中心")),
      body: FutureBuilder(
          future: getUserSimpleInfo(),
          builder: (context, response) {
            if (response.hasData) {
              // 数据model化
              userInfoModel = UserInfoModel.fromJson(response.data['data']);
              return Container(
                color: Colors.black12,
                child: ListView(
                  scrollDirection: Axis.vertical,
                  children: <Widget>[
                    _pictureFrame(),
                    // 我的订单按钮
                    BannerButton(
                      mainIcon: Icons.list,
                      mainShowMessage: "我的订单",
                      onTap: () {
                        print("点击我的订单.......");
                      },
                    ),
                    // 按钮行
                    IconLine(
                      iconList: <IconData>[
                        Icons.party_mode,
                        Icons.query_builder,
                        Icons.directions_car,
                        Icons.content_paste,
                      ],
                      showStringList: <String>[
                        "待付款",
                        "待发货",
                        "待收货",
                        "待评价",
                      ],
                      clickFunctionList: [
                        () {
                          print("点击了待付款.......");
                        }
                      ],
                    ),
                    Container(
                      margin: EdgeInsets.only(top: ScreenUtil().setHeight(8)),
                      child: // 领取优惠券
                          BannerButton(
                        mainIcon: Icons.blur_circular,
                        mainShowMessage: "领取优惠券",
                        onTap: () {
                          print("点击领取优惠券.......");
                        },
                      ),
                    ),
                    // 已领取优惠券
                    BannerButton(
                      mainIcon: Icons.blur_circular,
                      mainShowMessage: "已领取优惠券",
                      onTap: () {
                        print("点击已领取优惠券.......");
                      },
                    ),
                    // 地址管理
                    BannerButton(
                      mainIcon: Icons.location_on,
                      mainShowMessage: "地址管理",
                      onTap: () {
                        print("点击地址管理.......");
                      },
                    ),
                    //客服电话
                    Container(
                      margin: EdgeInsets.only(top: ScreenUtil().setHeight(8)),
                      child: BannerButton(
                        mainIcon: Icons.call,
                        mainShowMessage: "客服电话",
                        secondShowMessage: "2293785363",
                        onTap: () {
                          print("点击拨打客服电话.......");
                          _laucherPhone("2293785363");
                        },
                      ),
                    ),
                  ],
                ),
              );
            } else {
              return waitWidget;
            }
          }),
    );
  }

//cate 方法
  // 打电话
  void _laucherPhone(phone) async {
    String url = "tel:" + phone;
    // 可以打电话
    if (await canLaunch(url)) {
      // TODO: 暂时模拟打电话
      await launch(url);
    }
    // 不能
    else {
      throw 'Could not lauch $url';
    }
  }

  //cate 组件
  // 顶部头像框
  Widget _pictureFrame() {
    return Container(
      child: Container(
        padding: EdgeInsets.only(bottom: ScreenUtil().setHeight(6)),
        child: Stack(
          children: <Widget>[
            // 背景板
            Container(
              width: ScreenUtil().setWidth(Constant.MAX_WIDTH_SCREEN),
              height: ScreenUtil().setHeight(158),
              color: Colors.green,
              child: null,
            ),
            Positioned(
              top: ScreenUtil().setHeight(35),
              left: ScreenUtil().setWidth(107),
              child: Container(
                child: Container(
                  width: ScreenUtil().setHeight(65),
                  height: ScreenUtil().setHeight(65),
                  child: ClipOval(
                    child: Image.network(
                      userInfoModel.userInfo.picture,
                      fit: BoxFit.fill,
                    ),
                  ),
                ),
              ),
            ),
            Positioned(
              top: ScreenUtil().setHeight(117),
              child: Container(
                width: ScreenUtil().setWidth(Constant.MAX_WIDTH_SCREEN),
                child: Text(
                  userInfoModel.userInfo.nickname,
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: ScreenUtil().setSp(13),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
